//
//  ServiceBuyerViewModel.m
//  GLGW_Shoping
//
//  Created by yang on 2018/12/10.
//  Copyright © 2018年 yang. All rights reserved.
//

#import "ServiceBuyerViewModel.h"
#import "ServiceBuyseTableViewCell.h"
#import "NoServiceBuyersView.h"

@interface ServiceBuyerViewModel ()<UITableViewDelegate,UITableViewDataSource>
{
    UIView * selfView;
    UITableView * selfTableView;
    NSMutableArray * serviceDataSoucre;
    //
    CGFloat nowTableViewHeight;
    GLGWButton * bottomBut;
}
@end

@implementation ServiceBuyerViewModel

-(instancetype)initWithViewToViewModel:(UITableView *)tableView theSuperView:(UIView *)superView{
    if (self = [super initWithViewToViewModel:tableView theSuperView:superView]) {
        serviceDataSoucre = [NSMutableArray arrayWithCapacity:0];
        selfView = superView;
        tableView.dataSource = self;
        tableView.delegate = self;
        tableView.rowHeight = kHeightProportion_In_iPhoneTriploid(118);
        [superView addSubview:tableView];
        nowTableViewHeight = tableView.height;
        selfTableView = tableView;
        [self requestServiseData];
    }
    return self;
}

-(void)noDataShow{
    if (serviceDataSoucre.count ==0) {
        selfTableView.height = nowTableViewHeight - kHeightProportion_In_iPhoneTriploid(44);
        NoServiceBuyersView * nodataView = [[NoServiceBuyersView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, kHeightProportion_In_iPhoneTriploid(260))];
        nodataView.Block_AddNewBuyers = ^{
            if (self.delegate && [self.delegate respondsToSelector:@selector(addNewBuyersAction:)]) {
                [self.delegate addNewBuyersAction:nil];
            }
        };
        [self showNoDataImageView:nodataView];
        if (bottomBut) {
            [bottomBut removeFromSuperview];
        }
    }else{
        [self removeNoDataImage];
        selfTableView.height = nowTableViewHeight- kHeightProportion_In_iPhoneTriploid(88);
        bottomBut = [[GLGWButton alloc] initWithFrame:CGRectMake(0, kY_With_View(selfTableView, 0), KScreenWidth, kHeightProportion_In_iPhoneTriploid(44)) hTitle:@"新增买家信息" titleLableFont:SYSTEMFONT(14) titleLableColor:[UIColor whiteColor] theCornerRadius:0 backgroundColr:kColor_Rule_Yellow inSuperView:selfView];
        bottomBut.image_Name = @"adress_gord";
        [bottomBut setImagePositionWithType:SSImagePositionTypeLeft spacing:kWidthProportion_In_iPhoneTriploid(10)];
        [bottomBut addTarget:self action:@selector(addAction) forControlEvents:UIControlEventTouchUpInside];
    }
}
-(void)addAction{
    if (self.delegate && [self.delegate respondsToSelector:@selector(addNewBuyersAction:)]) {
        [self.delegate addNewBuyersAction:nil];
    }
}

#pragma mark -- UITableViewDelegate,UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return serviceDataSoucre.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ServiceBuyseTableViewCell * cell = [ServiceBuyseTableViewCell cellWithTableView:tableView];
    cell.inforModel = serviceDataSoucre[indexPath.row];
    cell.Block_ServiceEdit = ^{
        if (self.delegate && [self.delegate respondsToSelector:@selector(addNewBuyersAction:)]) {
            [self.delegate addNewBuyersAction:self->serviceDataSoucre[indexPath.row]];
        }
    };
    return cell;
}
#pragma mark -- 数据请求
-(void)requestServiseData{
    @weakify(self)
    _service_RecommendCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
        @strongify(self)
        RACSignal * requestSignal = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            NSMutableDictionary * parDic = (NSMutableDictionary *)input;
            if ([[parDic objectForKey:@"pageNo"] integerValue] == 1) {
                [self->serviceDataSoucre removeAllObjects];
            }
            [GLGWRequestTool POST:[NSString stringWithFormat:@"%@%@",kApiPrefix,kGetserviceBuyerList] lodingView:nil lodingTextString:@"" isLoding:YES parameters:parDic success:^(id  _Nullable response) {
                [subscriber sendNext:response];
                [subscriber sendCompleted];
            } failure:^(NSError * _Nullable error) {
                [subscriber sendNext:error];
                [subscriber sendCompleted];
            }];
            return nil;
        }];
        return [requestSignal map:^id _Nullable(id  _Nullable value) {
            ServiceBuyersModel * seiveModel = [ServiceBuyersModel yy_modelWithJSON:value];
            if (seiveModel.code == 0) {
                [self->serviceDataSoucre addObjectsFromArray:seiveModel.data.infos];
                [self noDataShow];
                [self->selfTableView reloadData];
            }
            return seiveModel;
        }];
    }];
    
    
}

@end
